;(function () {
  var projectIds = hlUrlUtil.GetQueryString('projectIds');
  var userId = hlStorage.getUserMes('id');

  function toDou(num) {
        return num < 10 ? '0' + num : '' + num;
    }

  function time(oldTime) {
      // 此刻的时间
      if (!oldTime) return '00天00小时';
      var oDate = new Date();

      var iS = parseInt(((oDate.getTime()+8*60*60*1000) - oldTime) / 1000);
      var iD = parseInt(iS / 86400); // 天数
      iS %= 86400; // 转换成天数剩余的秒数 -> 小时
      var iH = parseInt(iS / 3600); // 小时
      iS %= 3600; // 转换成小时数剩余的秒数 -> 分钟
      var iM = parseInt(iS / 60); // 分钟
      iS %= 60; // 秒数

      return toDou(iD)+'天'+toDou(iH)+'小时'
  }

  new Vue({
    el: '#myFlowIndex',
    data: {
      data: [],
      todoState: [{name: '全部',code: '#'}],
			selectedData: [],
      subLiData:[{name: '全部',code: '#'},{name: '我发起的',code: 'Y'},{name: '我处理的',code: 'N'}],
      pageNums: [{name: '10', code: '10'}, {name: '15', code: '15'}, {name: '20', code: '20'}],
      info: '',
      limit: ['10'],
      flag: '#',
      pageNum: {
        totalPage: 0,
        totalCount: 0
      },
      userid: userId,
			currentPage: 1,
			projectList: [],
			buildingList: [],
			floorList: [],
			unitList: [],
			projectId: [],
			buildingId: [],
			floorId: [],
			unitId: [],
			showView: false,
			loadingGif: false
    },
    filters: {
      tozero: function (n) {
        return n < 10 ? '0' + n : '' + n;
      },
      flowState: function (str) {
        var result;
        switch (str) {
          case 'FN':
            result = '已完结'
            break;
          case 'ON':
            result = '进行中'
            break;
          case 'DL':
            result = '已作废'
            break;
          default:
            result = '--'
        }
        return result;
      },
      timeFilter: function(time) {
        return hlDateUtil.formatUTCDate(time, 'yyyy-MM-dd HH:mm:ss')
      }
    },
    methods: {
			//翻页 
			handleChange (num) {		
				this.loadingGif = true;
				this.todoState.map(item => {
					if (item.code == this.selectedData[0]) {
					  this.getData(num, item.code)
					}
				})
			},
			checkBpm (arr) {
				this.loadingGif = true;
				this.currentPage = 1
				this.getData(1, arr[0].code)
			},
      //催办
      cuiban: function (id , taskId) {
        var self = this;
        hlhttp.get({
          url: 'bpm/urge/time',
          data: {bpm_id: id},
          success: function (res) {
            hllayer.promptSm({
              title:'催办',
              leftBtn:'确定',
              subTitle: '本操作将给处理人发送一条微信消息！',
              tips: '距离上次催办'+time(res.time),
              confirm:function (msg) {
                hlhttp.put({
                  url: 'bpm/urge',
                  data: {bpm_id: id, task_id: taskId, memo: msg || '#'},
                  success: function () {
                    hl.toast('发送成功')
                    $('.hllayer-prompt,.hllayer-mask').remove()
										self.loadingGif = true;
                    self.getData(self.currentPage, self.selectedData[0])
                  }
                })
              }
            })
          }
        })
      },
      flowClickNav: function (state) {
				this.loadingGif = true;
        this.flag = state;
				this.currentPage = 1
				this.getData(this.currentPage, this.selectedData[0])
      },
      //获取流程唯一标识列表
      bpm: function () {
        var _this = this;
        hlhttp.get({
          url: 'bpm_keys',
          success: function (res) {
						let newData = []
						let userinfo = JSON.parse(localStorage.getItem('userMes'))
						newData = hlArrayUtil.replaceData(res, [{key: 'memo', rKey: 'name'}])
						if (userinfo.preSysCode.indexOf('SINOOCEAN') == -1) {
							newData.map((item, index) => {
								if (item.dictValue == 'RRAT') {
									newData.splice(index, 1)
								}
							})
            }
            newData.map(item =>{
              _this.todoState.push({
                name:item.name,
                code:item.value
              })
            })
            // _this.todoState = newData
            
						_this.selectedData = ['#']
						_this.getData(_this.currentPage, _this.todoState[0].code)
          }
        })
      },
      //获取流程列表数据
      getData: function (nums, state) {
        var _this = this;
        // var myself = this.flag ? 'Y' : 'N';
        hlhttp.get({
          url: 'bpms',
          data: {
            bpm_key: state || '#',
            keyword: _this.info.trim() || '#',
            myself: _this.flag,
            page: nums,
            limit: _this.limit[0],
            project_id: this.projectId.length ? this.projectId[0] : '#',
            building_id: this.buildingId.length ? this.buildingId[0] : '#',
            floor_id: this.floorId.length ? this.floorId[0] : '#',
            unit_id: this.unitId.length ? this.unitId[0] : '#',
          },
          success: function(res) {
						_this.loadingGif = false;
            _this.pageNum = {totalPage: res.totalPage, totalCount:res.totalCount}
            _this.data = res.bpms;            
          }
        })
      },      
      getDateTime: function() {
        var _this = this;
				this.currentPage = 1
        setTimeout(function() {          
          _this.getData(1, _this.selectedData[0])
        }, 100);
      },
      reset: function () {
        this.info = '';
        this.flag = '#';
				this.projectId = []
				this.buildingId = []
				this.floorId = []
				this.unitId = []
				this.buildingList = []
				this.floorList = []
				this.unitList = []
				this.currentPage = 1
				this.selectedData = ['#']
        this.getData(1, this.todoState[0].code)
      },
      // 前往合同流程
      goToContractFLow:function (businessId,bpmId,taskId,bpmKey) {
        var parame = {
          projectIds:projectIds,
          bpmId:bpmId,
          taskId:taskId,
          bpmKey:bpmKey,
          businessId:businessId,
          page:'XQ',
          ddtab:true
        }
        hlUrlUtil.goTo('contractFLow',parame)
      },
      // 前往意向流程
      goToITFLow:function (businessId,bpmId,taskId,bpmKey) {
        var parame = {
          projectIds:projectIds,
          bpmId:bpmId,
          taskId:taskId,
          bpmKey:bpmKey,
          businessId:businessId,
          page:'XQ',
          ddtab:true
        }
        hlUrlUtil.goTo('intentionFLow',parame)
      },
			// 前往账单流程
			tobill:function (businessId,bpmId,taskId,bpmKey) {
			  var parame = {
			    projectIds:projectIds,
			    bpmId:bpmId,
			    taskId:taskId,
			    bpmKey:bpmKey,
			    businessId:businessId,
			    page:'XQ',
			    ddtab:true
			  }
			  hlUrlUtil.goTo('billFlow',parame)
			},
      // 前往退租流程
      goToWithdrawFlow:function (businessId,bpmId,taskId,bpmKey) {
        var parame = {
          projectIds:projectIds,
          bpmId:bpmId,
          taskId:taskId,
          bpmKey:bpmKey,
          businessId:businessId,
          page:'XQ',
          ddtab:true
        }
        hlUrlUtil.goTo('withdrawFlow',parame)
      },
			// 前往退租流程
			gotoRRATpage:function (businessId,bpmId,taskId,bpmKey,type) {
			  var parame = {
			    projectIds:projectIds,
			    bpmId:bpmId,
			    taskId:taskId,
			    bpmKey:bpmKey,
			    businessId:businessId,
          page:'XQ',
          type:type,
			    ddtab:true
			  }
			  hlUrlUtil.goTo('rratFlow',parame)
			},
			// 前往变更流程
			cctFlowPage:function (businessId,bpmId,taskId,bpmKey) {
			  var parame = {
			    projectIds:projectIds,
			    taskId:taskId,
			    bpmId:bpmId,
			    bpmKey:bpmKey,
			    businessId:businessId,
			    ddtab:true
			  }
			  hlUrlUtil.goTo('contractFlowChange',parame)
			},
      //撤回
      withdrawCH: function (id) {
        var _this = this;
        hllayer.confirm1('撤回后，流程将立即终止', function() {
          hlhttp.delete({
            url: 'bpm/_withdraw?bpm_id='+id,
            success: function () {
              hl.toast('撤回成功')
							_this.loadingGif = true;
							 _this.getData(_this.currentPage, _this.selectedData[0])							            
            }
          })
        })
      },
			openSearch: function() {
			  if (this.showView == true) {
			    this.showView = false;
					this.projectId = []
					this.buildingId = []
					this.floorId = []
					this.unitId = []
					this.buildingList = []
					this.floorList = []
					this.unitList = []
			    return false;
			  }
			  if (this.showView == false) {
			    this.showView = true;
			    return false;
			  }
			},
			//获取项目
			getprojectData () {
			  hlhttp.get({
			    url: 'user/projects',
			    success: (res) => {
						res = hlArrayUtil.removeRepeat(res, 'projectId')						
			      this.projectList = hlArrayUtil.replaceData(res, [{key: 'projectName', rKey: 'name'}, {key: 'projectId', rKey: 'code'}]);
			    }
			  })
			},
			//获取楼宇
			getbuildingData () {
			  hlhttp.get({
			    url: 'project/buildings',
			    data: {project_id: this.projectId[0]},
			    success: (res) => {
			      this.buildingList = hlArrayUtil.replaceData(res, [{key: 'buildingId', rKey: 'code'}]);
			    }
			  })
			},
			//获取楼层
			getfloorData () {
			  hlhttp.get({
			    url: 'building/floors',
			    data: {project_id: this.projectId[0], building_id: this.buildingId[0]},
			    success: (res) => {
			      this.floorList = hlArrayUtil.replaceData(res, [{key: 'floorName', rKey: 'name'}, {key: 'floorId', rKey: 'code'}]);
			    }
			  })
			},
			//获取单元
			getunitData () {
			  hlhttp.get({
			    url: 'floor/units',
			    data: {project_id: this.projectId[0], building_id: this.buildingId[0], floor_id: this.floorId[0]},
			    success: (res) => {
			      this.unitList = hlArrayUtil.replaceData(res, [{key: 'unitName', rKey: 'name'}, {key: 'unitId', rKey: 'code'}]);
			    }
			  })
			},
			//点击项目获得  id
			getprojectId (arr) {
			  this.projectId = [arr[0].code];
			  this.getbuildingData()
			  this.buildingId = [];
			  this.floorId = [];
			  this.unitId = [];
			},
			//点击楼宇获得 id
			getbuildingId (arr) {
			  this.buildingId = [arr[0].code];
			  this.getfloorData()
			  this.floorId = [];
			  this.unitId = [];
			},
			//点击楼层获得 id
			getfloorId (arr) {
			  this.floorId = [arr[0].code];
			  this.getunitData()
			  this.unitId = [];
			},
			// 点击单元获得 id
			getunitId (arr) {
			  this.unitId = [arr[0].code];
			},
    },
    mounted: function () {
      hlSelectUi(true)
			this.getprojectData()
      this.bpm()
    }
  })
} ())
